package com.demo.mqtt.common.utils;

import org.apache.commons.codec.binary.Base64;

/**
 * @Name: CodeUtil
 * @Description: CodeUtil
 **/
public class CodeUtil {
    /**
     * hex string to base64 string
     *
     * @param hexStr
     * @return
     */
    public static String hexStringToBase64String(String hexStr) {
        return byteArrayToBase64String(hexStringToByteArray(hexStr));
    }

    /**
     * base64 string to byte[]
     *
     * @param base64Str
     * @return byte[]
     */
    public static byte[] base64StringToByteArray(String base64Str) {
        return Base64.decodeBase64(base64Str);
    }

    /**
     * byte[] To base64 String
     *
     * @param byteArray
     * @return string
     */
    private static String byteArrayToBase64String(byte[] byteArray) {
        return Base64.encodeBase64String(byteArray);
    }

    /**
     * hex string to byte
     *
     * @param hexStr
     * @return
     */
    public static byte hexStringToByte(String hexStr, int index) {
        byte highDit = (byte) (Character.digit(hexStr.charAt(index), 16) & 0xFF);
        byte lowDit = (byte) (Character.digit(hexStr.charAt(index + 1), 16) & 0xFF);
        return (byte) (highDit << 4 | lowDit);
    }

    /**
     * hex string to byte[]
     *
     * @param hexStr
     * @return byte[]
     */
    public static byte[] hexStringToByteArray(String hexStr) {
        final byte[] byteArray = new byte[hexStr.length() >> 1];
        int index = 0;
        for (int i = 0; i < hexStr.length(); i++) {
            if (index > hexStr.length() - 1) {
                return byteArray;
            }
            byteArray[i] = hexStringToByte(hexStr, index);
            index += 2;
        }
        return byteArray;
    }

    /**
     * byte to hex string： 1 byte
     *
     * @param b
     * @return string
     */
    public static String byteToHexString(byte b) {
        int v = b & 0xFF;
        String hv = Integer.toHexString(v);
        if (hv.length() < 2) {
            hv = "0" + hv;
        }
        return hv;
    }

    /**
     * hex string to ascii string
     *
     * @param hexStr
     * @return
     */
    public static String hexStringToAsciiString(String hexStr) {
        StringBuilder output = new StringBuilder();
        for (int i = 0, len = hexStr.length(); i < len; i += 2) {
            output.append((char) Integer.parseInt(hexStr.substring(i, i + 2), 16));
        }
        return output.toString();
    }

    /**
     * ascii string to hex string
     *
     * @param asciiStr
     * @return
     */
    public static String asciiStringToHexString(String asciiStr) {
        char[] chars = asciiStr.toCharArray();
        StringBuilder sbHex = new StringBuilder();
        for (char ch : chars) {
            sbHex.append(Integer.toHexString(ch));
        }
        return sbHex.toString();
    }

    /**
     * byte[] to hex string
     *
     * @param bytes
     * @return
     */
    public static String bytesToHex(byte[] bytes) {
        StringBuilder buf = new StringBuilder(bytes.length * 2);
        for (byte b : bytes) { // 使用String的format方法进行转换
            buf.append(String.format("%02x", new Integer(b & 0xff)));
        }

        return buf.toString();
    }

    /**
     * 将16进制字符串拆分为 16进制 String[]
     *
     * @param str
     * @return
     */
    public static String[] toBytes(String str) {
        if (str == null || str.trim().equals("")) {
            return new String[0];
        }

        String[] bytes = new String[str.length() / 2];
        for (int i = 0; i < str.length() / 2; i++) {
            String subStr = str.substring(i * 2, i * 2 + 2);
            bytes[i] = subStr;
        }

        return bytes;
    }

    /**
     * byte数组转换成16进制字符串
     *
     * @param src
     * @return
     */

    public static String bytesToHexString(byte[] src) {
        StringBuilder stringBuilder = new StringBuilder();
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }

    /**
     * byte数组转换成16进制字符数组
     *
     * @param src
     * @return
     */

    public static String[] bytesToHexStrings(byte[] src) {
        if (src == null || src.length <= 0) {
            return null;
        }
        String[] str = new String[src.length];
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                str[i] = "0";
            }
            str[i] = hv;
        }
        return str;
    }


    /**
     * BCD码转为10进制串(阿拉伯数据)
     * @param BCD码
     * @return 10进制串
     */
    public static String bcd2Str(byte[] bytes) {
        StringBuffer temp = new StringBuffer(bytes.length * 2);
        for (int i = 0; i < bytes.length; i++) {
            temp.append((byte) ((bytes[i] & 0xf0) >>> 4));
            temp.append((byte) (bytes[i] & 0x0f));
        }
        return temp.toString().substring(0, 1).equalsIgnoreCase("0") ? temp
                .toString().substring(1) : temp.toString();
    }

    /**
     * 10进制串转为BCD码
     * @param 10进制串
     * @return BCD码
     */
    public static byte[] str2Bcd(String asc) {
        int len = asc.length();
        int mod = len % 2;
        if (mod != 0) {
            asc = "0" + asc;
            len = asc.length();
        }
        byte abt[] = new byte[len];
        if (len >= 2) {
            len = len / 2;
        }
        byte bbt[] = new byte[len];
        abt = asc.getBytes();
        int j, k;
        for (int p = 0; p < asc.length() / 2; p++) {
            if ((abt[2 * p] >= '0') && (abt[2 * p] <= '9')) {
                j = abt[2 * p] - '0';
            } else if ((abt[2 * p] >= 'a') && (abt[2 * p] <= 'z')) {
                j = abt[2 * p] - 'a' + 0x0a;
            } else {
                j = abt[2 * p] - 'A' + 0x0a;
            }
            if ((abt[2 * p + 1] >= '0') && (abt[2 * p + 1] <= '9')) {
                k = abt[2 * p + 1] - '0';
            } else if ((abt[2 * p + 1] >= 'a') && (abt[2 * p + 1] <= 'z')) {
                k = abt[2 * p + 1] - 'a' + 0x0a;
            } else {
                k = abt[2 * p + 1] - 'A' + 0x0a;
            }
            int a = (j << 4) + k;
            byte b = (byte) a;
            bbt[p] = b;
        }
        return bbt;
    }


}
